package com.ttdev.mmms.controller.login;


import com.ttdev.mmms.config.SecurityUtils;
import com.ttdev.mmms.entity.MemberInfo;
import com.ttdev.mmms.repository.MemberRepository;
import com.ttdev.mmms.service.MemberLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 会员登录
 * 王旭国
 */
@Controller
@RequestMapping("/memberLogin")
public class MemberLoginController {

    @Autowired
    private  MemberRepository memberRepository;

    @RequestMapping("/login")
    public String login(String username,String password,HttpSession session,RedirectAttributes attr) {
        Integer iphone;
        MemberInfo member;
        String pwd = SecurityUtils.md5Hex(password);//MD5技术加密
//        MemberInfo member= memberRepository.findPwd(username);
      /*  try {
            //String类型转换Integer类型
            iphone=Integer.parseInt(username);
            System.out.println(iphone);
            member= memberRepository.selectMemberIphone(iphone);
        }catch (Exception e){
            e.printStackTrace();
            member= memberRepository.findPwd(username);
        }*/
        member= memberRepository.findPwd(username);
        if (member!=null){
            if(member.getPassword().equals(pwd)){
                session.setAttribute("member",member);
                //以秒为单位，即在没有活动90分钟后，session将失效
                session.setMaxInactiveInterval(90*60);
                return "redirect:/memberIndex";
            }else {
                attr.addFlashAttribute("error", "密码错误!");
                return "redirect:/";
            }
        }else{
            attr.addFlashAttribute("error", "账号不存在!");
            return "redirect:/";
        }
    }
}